package server.content.skills;

import server.Config;
import server.game.players.Client;

/**
 * RuneCrafting.java
 * 
 * @author Izumi
 * 
 **/

public class Runecrafting {

	private Client c;

	public Runecrafting(Client c) {
		this.c = c;
	}

	private enum Altars {
		AIR_ALTAR(2452, new int[] { 1438, 5527 }, new int[] { 2842, 4829 }), MIND_ALTAR(
				2453, new int[] { 1448, 5529 }, new int[] { 2793, 4828 }), WATER_ALTAR(
				2454, new int[] { 1444, 5531 }, new int[] { 2713, 4836 }), EARTH_ALTAR(
				2455, new int[] { 1440, 5535 }, new int[] { 2655, 4831 }), FIRE_ALTAR(
				2456, new int[] { 1442, 5537 }, new int[] { 2577, 4845 }), BODY_ALTAR(
				2457, new int[] { 1446, 5533 }, new int[] { 2521, 4834 }), COSMIC_ALTAR(
				2458, new int[] { 1454, 5539 }, new int[] { 2162, 4833 }), CHAOS_ALTAR(
				2461, new int[] { 1452, 5543 }, new int[] { 2268, 4842 }), NATURE_ALTAR(
				2460, new int[] { 1462, 5541 }, new int[] { 2400, 4835 }), LAW_ALTAR(
				2459, new int[] { 1458, 5545 }, new int[] { 2464, 4819 }), DEATH_ALTAR(
				2462, new int[] { 1456, 5547 }, new int[] { 2208, 4831 });

		int objId;
		int[] keys, loc;

		private Altars(int objId, int[] keys, int[] loc) {
			this.objId = objId;
			this.loc = loc;
			this.keys = keys;
		}

		private int getObj() {
			return objId;
		}

		private int[] getKeys() {
			return keys;
		}

		private int[] getNewLoc() {
			return loc;
		}
	}

	public Altars forAltar(int id) {
		for (Altars a : Altars.values()) {
			if (a.getObj() == id) {
				return a;
			}
		}
		return null;
	}

	public void enterAltar(int objId, int itemUse) {
		Altars a = forAltar(objId);
		if (a != null) {
			if (a.getKeys()[1] == c.playerEquipment[c.playerHat]
					|| a.getKeys()[0] == itemUse) {
				c.getPA().movePlayer(a.getNewLoc()[0], a.getNewLoc()[1], 0);
				c.sendMessage("You enter the mysterious ruins.");
			} else {
				c.sendMessage("Nothing interesting happens.");
			}
		}
	}

	public void leaveAltar(final int id) {
		switch (id) {
		case 2465: // Air Altar Portal
			c.teleportToX = 2986;
			c.teleportToY = 3295;
			break;
		case 2466: // Mind Altar
			c.teleportToX = 2980;
			c.teleportToY = 3511;
			break;
		case 2470: // Body Altar
			c.teleportToX = 3055;
			c.teleportToY = 3443;
			break;
		case 2467: // Water
			c.getPA().movePlayer(3182, 3165, 0);
			break;
		case 2468: // Earth
			c.getPA().movePlayer(3304, 3472, 0);
			break;
		case 2469: // Fire
			c.getPA().movePlayer(3311, 3252, 0);
			break;
		case 2471: // Cosmic
			c.getPA().movePlayer(2406, 4379, 0);
			break;
		case 2473: // Nature
			c.getPA().movePlayer(2867, 3020, 0);
			break;
		case 2474: // Chaos
			c.getPA().movePlayer(3062, 3590, 0);
			break;
		case 2472: // Law
			c.getPA().movePlayer(2858, 3378, 0);
			break;
		}
	}

	/**
	 * Rune essence ID constant.
	 */
	private static final int RUNE_ESS = 1436;

	/**
	 * Pure essence ID constant.
	 */
	private static final int PURE_ESS = 7936;

	/**
	 * An array containing the rune item numbers.
	 */
	public int[] runes = { 556, 558, 555, 557, 554, 559, 564, 562, 561, 563,
			560, 565 };

	/**
	 * An array containing the object IDs of the runecrafting altars.
	 */
	public int[] altarID = { 2478, 2479, 2480, 2481, 2482, 2483, 2484, 2487,
			2486, 2485, 2488, 2489 };

	/**
	 * 2D Array containing the levels required to craft the specific rune.
	 */
	public int[][] craftLevelReq = { { 556, 1 }, { 558, 2 }, { 555, 5 },
			{ 557, 9 }, { 554, 14 }, { 559, 20 }, { 564, 27 }, { 562, 35 },
			{ 561, 44 }, { 563, 54 }, { 560, 65 }, { 565, 77 } };

	/**
	 * 2D Array containing the levels that you can craft multiple runes.
	 */
	public int[][] multipleRunes = { { 11, 22, 33, 44, 55, 66, 77, 88, 99 },
			{ 14, 28, 42, 56, 70, 84, 98 }, { 19, 38, 57, 76, 95 },
			{ 26, 52, 78 }, { 35, 70 }, { 46, 92 }, { 59 }, { 74 }, { 91 },
			{ 100 }, { 100 }, { 100 } };

	public int[] runecraftExp = { 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 11 };

	/**
	 * Checks through all 28 item inventory slots for the specified item.
	 */
	private boolean itemInInv(int itemID, int slot, boolean checkWholeInv) {
		if (checkWholeInv) {
			for (int i = 0; i < 28; i++) {
				if (c.playerItems[i] == itemID + 1) {
					return true;
				}
			}
		} else {
			if (c.playerItems[slot] == itemID + 1) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Replaces essence in the inventory with the specified rune.
	 */
	private void replaceEssence(int essType, int runeID, int multiplier,
			int index) {
		System.out.println("multipler: " + multiplier);
		int exp = 0;
		for (int i = 0; i < 28; i++) {
			if (itemInInv(essType, i, false)) {
				c.getItems().deleteItem(essType, i, 1);
				c.getItems().addItem(runeID, 1 * multiplier);
				exp += runecraftExp[index];
			}
		}
		c.getPA().addSkillXP(exp * Config.RUNECRAFTING_EXPERIENCE,
				c.playerRunecrafting);
	}

	/**
	 * Crafts the specific rune.
	 */
	public void craftRunes(int altarID) {
		int runeID = 0;

		for (int i = 0; i < this.altarID.length; i++) {
			if (altarID == this.altarID[i]) {
				runeID = runes[i];
			}
		}
		for (int i = 0; i < craftLevelReq.length; i++) {
			if (runeID == runes[i]) {
				if (c.playerLevel[20] >= craftLevelReq[i][1]) {
					if (c.getItems().playerHasItem(RUNE_ESS)
							|| c.getItems().playerHasItem(PURE_ESS)) {
						int multiplier = 1;
						for (int j = 0; j < multipleRunes[i].length; j++) {
							if (c.playerLevel[20] >= multipleRunes[i][j]) {
								multiplier += 1;
							}
						}
						replaceEssence(RUNE_ESS, runeID, multiplier, i);
						c.startAnimation(791);
						// c.frame174(481, 0, 0); for sound
						c.gfx100(186);
						return;
					}
					c.sendMessage("You need to have essence to craft runes!");
					return;
				}
				c.sendMessage("You need a higher runecrafting level to craft this rune.");
			}
		}
	}

}